package com.app.megaprosoft.modyimcarcare;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import com.app.megaprosoft.DB.Ar;
import com.app.megaprosoft.DB.ArGroup;
import com.app.megaprosoft.DB.ArGroupMain;
import com.app.megaprosoft.DB.CarBand;
import com.app.megaprosoft.DB.CarColor;
import com.app.megaprosoft.DB.CarGeneration;
import com.app.megaprosoft.DB.CarGroup;
import com.app.megaprosoft.DB.Company;
import com.app.megaprosoft.DB.Connect;
import com.app.megaprosoft.DB.FFUser;
import com.app.megaprosoft.DB.Item;
import com.app.megaprosoft.DB.ItemGroup;
import com.app.megaprosoft.DB.ItemGroupMain;
import com.app.megaprosoft.DB.ItemPrice;
import com.app.megaprosoft.DB.ItemPromotionPrices;
import com.app.megaprosoft.DB.ItemSize;
import com.app.megaprosoft.DB.ItemStock;
import com.app.megaprosoft.DB.ItemUnit;
import com.app.megaprosoft.DB.Province;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.app.megaprosoft.DB.SQLiteDB_ArFF;
import com.app.megaprosoft.DB.SQLiteDB_POS;
import com.app.megaprosoft.DB.SQLiteDB_POSList;
import com.app.megaprosoft.DB.ServicePOS;
import com.app.megaprosoft.DB.UserGroup;
import com.app.megaprosoft.general.DateTime;
import com.app.megaprosoft.modyimcarcare.Carcare_StatusChange.LoadingLogin;
import com.megaprosoft.modyimcarcare.R;

import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


@SuppressLint("SimpleDateFormat")
public class Carcare_Main extends Activity {
//	public WifiManager 						wifi ;
	public Boolean							WifiEnable;
	public static final int 				DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog 					mProgressDialog;
	private Animation 						shake;
//	AlertDialog alert;
	private Button   						_Button_Service,
											_Button_Showlog,
											_Button_Updatedata,
											_Button_Exit;
	private ImageView   					_ImageView_Update;
	private TextView 						_TextView_company,
											_TextView_username,
											_TextView_defname,
											_TextView_service,
											_TextView_check,
											_TextView_all,
											_TextView_date;
	//Dialog
	private TextView 						tv_Password;
	private Button 							button_1,button_2,button_3,button_4,button_5,
											button_6,button_7,button_8,button_9,button_0,
											button_clrsr,button_cancle,button_enter,button_del;
	
	private String   						FFUserOID
											,UserName
											,Password
											,DepCode
											,DepName
											,UserGroupOID
											,UserGroupName
											,PicPath
											,UserGroupCode="A"
											,ListService[][]
											,TabletCode
											,user_Password;
	final	int								dialog_help			= 0,
											dialog_setting		= 1,
											dialog_DefineData 	= 3,
											dialog_Updatetoday 	= 4;
	public  int								updatecase			= 0;
	public	int								Dialog_Mode=3;
	public  long							CheckUpdate;
	private Toast 							mToast;
	private ProgressBar 					progressBar;
	private ListView 						Carcare_ServiceListView;
	
	//Intent
	private Intent  						GetCar;
	private Intent  						ShowLog;
	
	private Boolean 						updateall 			= false;
	private Boolean 						update 				= true;
	private AlertDialog 					loginDialog;
	private CarCare_Adapter_ListService 	carcare_listservice_adapter;
	
	private Carcare_SessionManagement		session;
	private SQLiteDB_ArFF 					sqlite_ar;
	private SQLiteDB_POS 					sqlite_pos;
	private SQLiteDB_POSList 				sqlite_poslist;
	//Database Service
	
	private UserGroup usergroup = new UserGroup();
	private Ar ar = new Ar();

	private ServicePOS servicepos = new ServicePOS();
	private FFUser ffuser = new FFUser();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.);
		setContentView(R.layout.a_carcare_activity_main);
		setTitle("ศูนย์เอาใจใส่รถยนต์มดขยัน");
		//Content SQLite
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);

		
		servicepos.setContext(getApplicationContext());
		ar.setContext(getApplicationContext());
		
		_TextView_company 	= (TextView) findViewById(R.id.a_main_company);
		_TextView_username = (TextView) findViewById(R.id.a_main_username);
		_TextView_defname 	= (TextView) findViewById(R.id.a_main_depart);
		_TextView_date 	= (TextView) findViewById(R.id.a_main_date);
		
		_TextView_service 	= (TextView) findViewById(R.id.a_main_service);
		_TextView_check	= (TextView) findViewById(R.id.a_main_check); 
		_TextView_all 		= (TextView) findViewById(R.id.a_main_all);
		
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		
		session = new Carcare_SessionManagement(getApplicationContext());
		session.checkLogin();
        HashMap<String, String> user = session.getUserDetails();

        FFUserOID 		= user.get(Carcare_SessionManagement.KEY_FFUserOID);
        UserName 		= user.get(Carcare_SessionManagement.KEY_UserName);
        Password		= user.get(Carcare_SessionManagement.KEY_Password);
        DepCode 		= user.get(Carcare_SessionManagement.KEY_DepCode);
        DepName 		= user.get(Carcare_SessionManagement.KEY_DepName);
        UserGroupOID 	= user.get(Carcare_SessionManagement.KEY_UserGroupOID);
        UserGroupName 	= user.get(Carcare_SessionManagement.KEY_UserGroupName);
        UserGroupCode 	= user.get(Carcare_SessionManagement.KEY_UserGroupCode);
        PicPath 		= user.get(Carcare_SessionManagement.KEY_PicPath);
        TabletCode 		= user.get(Carcare_SessionManagement.KEY_TabletCode);
        
        
        _TextView_company.setText(UserGroupName);
        _TextView_username.setText(UserName);
        _TextView_defname.setText(DepName);
        _TextView_service.setText("-");
        _TextView_check.setText("-");
        _TextView_all.setText("-");
				
//////////////////////////////////////// Button /////////////////////////////		
		_Button_Service = (Button) findViewById(R.id.carcare_service);
		_Button_Service.setOnClickListener(buttonService);
		_Button_Showlog = (Button) findViewById(R.id.carcare_reservice);
		_Button_Showlog.setOnClickListener(buttonsShowlog);
		
		_Button_Exit = (Button) findViewById(R.id.carcare_exit);
		_Button_Exit.setOnClickListener(buttonExit);
		_ImageView_Update = (ImageView) findViewById(R.id.imgprogressBar);
		_ImageView_Update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				long CheckUpdate[] =null;
				try {
					CheckUpdate = ar.UpdateSQLiteArFFFromServer();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				new UpdatePOS().execute();
				if(CheckUpdate.length>0){
//					Toast.makeText(Carcare_Main.this,String.valueOf(CheckUpdate.length), Toast.LENGTH_SHORT).show();
					Toast.makeText(Carcare_Main.this,"Row = "+String.valueOf(CheckUpdate[0])
							+" ,Insert = "+String.valueOf(CheckUpdate[1])
							+" ,Update = "+String.valueOf(CheckUpdate[2])
//							+" ,Error = "+String.valueOf(CheckUpdate[5])
							, Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(Carcare_Main.this,"Null", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		Calendar mCalendar = Calendar.getInstance();
		_TextView_date.setText(getFormat().format(mCalendar.getTime()));

		GetCar = new Intent(Carcare_Main.this,CarCare_GetCar.class);
		ShowLog = new Intent(Carcare_Main.this,Carcare_History.class);
		
		Carcare_ServiceListView = (ListView) findViewById(R.id.carcare_service_listservice);
		
		LayoutInflater inflater = Carcare_Main.this.getLayoutInflater();
		
        loginDialog = new AlertDialog.Builder(Carcare_Main.this)
        .setView(inflater.inflate(R.layout.a_carcare_dialog_confirmuser, null))
        .setCancelable(false)
        .create();
        
        loginDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				
				tv_Password = (TextView) loginDialog.findViewById(R.id.a_login_password);
				tv_Password.setText("");
				
				button_0 = (Button) loginDialog.findViewById(R.id.a_login_button0);
				button_0.setOnClickListener(text_0);
				button_1 = (Button) loginDialog.findViewById(R.id.a_login_button1);
				button_1.setOnClickListener(text_1);
				button_2 = (Button) loginDialog.findViewById(R.id.a_login_button2);
				button_2.setOnClickListener(text_2);
				button_3 = (Button) loginDialog.findViewById(R.id.a_login_button3);
				button_3.setOnClickListener(text_3);
				button_4 = (Button) loginDialog.findViewById(R.id.a_login_button4);
				button_4.setOnClickListener(text_4);
				button_5 = (Button) loginDialog.findViewById(R.id.a_login_button5);
				button_5.setOnClickListener(text_5);
				button_6 = (Button) loginDialog.findViewById(R.id.a_login_button6);
				button_6.setOnClickListener(text_6);
				button_7 = (Button) loginDialog.findViewById(R.id.a_login_button7);
				button_7.setOnClickListener(text_7);
				button_8 = (Button) loginDialog.findViewById(R.id.a_login_button8);
				button_8.setOnClickListener(text_8);
				button_9 = (Button) loginDialog.findViewById(R.id.a_login_button9);
				button_9.setOnClickListener(text_9);
				button_clrsr = (Button) loginDialog.findViewById(R.id.a_login_button_clrscr);
				button_clrsr.setOnClickListener(text_clrsr);
				button_enter = (Button) loginDialog.findViewById(R.id.a_login_button_enter);
				button_enter.setOnClickListener(text_enter);
				button_cancle = (Button) loginDialog.findViewById(R.id.a_login_button_cancle);
				button_cancle.setOnClickListener(text_cancle);
				button_del = (Button) loginDialog.findViewById(R.id.a_login_button_del);
				button_del.setOnClickListener(text_del);
				
			}
		});
        
		Define_ListService();
		
	}
	public SimpleDateFormat getFormat(){
//	    return new SimpleDateFormat("MMM, yyyy");
		return new java.text.SimpleDateFormat("dd MMMM yyyy");
	}; 

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
//		Toast.makeText(Mansion_Show.this, "9", Toast.LENGTH_SHORT).show();
		Define_ListService();
//		System.out.println("onWindowFocusChanged");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
//		Toast.makeText(Carcare_Main.this, "sResume", Toast.LENGTH_SHORT).show();
		ar.setContext(getApplicationContext());
		servicepos.setContext(getApplicationContext());
		ffuser.setContext(getApplicationContext());
		usergroup.setContext(getApplicationContext()); 
		new UpdatePOS().execute();
//		Define_ListService();
	}
	
	
	public void Define_ListService(){	
		carcare_listservice_adapter = new CarCare_Adapter_ListService(Carcare_Main.this, 
				R.layout.list_service, new ArrayList<CarCare_ListService>());
		Carcare_ServiceListView.setAdapter(carcare_listservice_adapter);
		int length = 0;
		try{
			ListService = servicepos.SelectAll_ServicePOS("0",UserGroupCode,"");
			length = ListService.length;
			_TextView_all.setText(String.valueOf(length));
		
		}catch(Exception e){
			
		}
		int Service=0;
		int check=0;
		for(int i=0;i<length;i++){
			try{
				String _idcar;
				if(ListService[i][2].equals("")){
					_idcar="ไม่ระบุ";
				}else{
					_idcar=ListService[i][2].toString();
				}
				carcare_listservice_adapter.insert(new CarCare_ListService( length-i,
																			ListService[i][1].toString(),
																			_idcar, 
																			ListService[i][5].toString(),
																			ListService[i][9].toString(),
																			ListService[i][8].toString(),
																			ListService[i][19].toString()),
																			0);
//				
				if(ListService[i][8].toString().equals("รอปล่อย")){
					check=check+1;
				}else{
					Service=Service+1;
				}
			}catch(Exception e){
				
			}
        }
		
		_TextView_check.setText(String.valueOf(check));
		_TextView_service.setText(String.valueOf(Service));
	
		
	}
	
	//----------------------Button Operation------------------------------//
    
	private OnClickListener buttonService = new OnClickListener() {
        public void onClick(View v) {
        	GetCar.putExtra("ServiceId", "");
        	GetCar.putExtra("MODE", "NEW");
			startActivity(GetCar);
        }
    };
    
    private OnClickListener buttonsShowlog = new OnClickListener() {
        public void onClick(View v) {
        	startActivity(ShowLog);
        }
    };
    

    private OnClickListener buttonExit = new OnClickListener() {
        public void onClick(View v) {
        	onBackPressed();
        }
    };
     
    @Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("ออกจากระบบ")
	        .setIcon(R.drawable.erroralert)
	        .setMessage("ต้องการออกจากระบบ")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                    	session.logoutUser();
	                    	Intent newActivity = new Intent(Carcare_Main.this,Carcare_Login.class);
	            			startActivity(newActivity);
	                        finish();
	                    }
	                })
	                .setPositiveButton(android.R.string.no, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                    	
	                    }
	                }).create().show();
	}
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setTitle("อัพเดทข้อมูล");
            mProgressDialog.setIcon(R.drawable.upload);
            mProgressDialog.setMessage("กำลังทำการอัพเดทข้อมูล.....");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
      
        default:
            return null;
        }
    }
    
 	//-----------AsyncTask-Loading----------------------------------//
    public class Update extends AsyncTask<String, Void, Void> {
        @SuppressWarnings("deprecation")
		protected void onPreExecute() {
        	super.onPreExecute();
        	updateall = false;
        	updatecase = 0;
//        	sqlite_pos = new SQLiteDB_POS(getApplicationContext());
//    		sqlite_pos.getWritableDatabase();
//    		sqlite_poslist = new SQLiteDB_POSList(getApplicationContext());
//    		sqlite_poslist.getWritableDatabase();
        	
        	showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	try {
				ar.UpdateSQLiteArFFFromServer();
			} catch (Exception e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
        	try {
				ar.UpdatetoServer_ArFF("");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try{
				if(updatecase>=0){
					CheckUpdate = servicepos.UpdatetoServer_POS("");
					if(CheckUpdate>=0){
						updatecase = 11;
					}else{
						updatecase = -11;
					}
				}
			} catch (Exception e1) {
				updatecase = -11;
			}
			try{
				if(updatecase>=0){
					CheckUpdate = servicepos.UpdatetoServer_POSLIST();
					if(CheckUpdate>=0){
						updatecase = 21;
					}else{
						updatecase = -21;
					}
				}
			} catch (Exception e2) {
				updatecase = -21;
			}
			
			
			updateall = true;
    		return null;
        }

        @SuppressWarnings("deprecation")
		protected void onPostExecute(Void unused) {
        	 dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
             removeDialog(DIALOG_DOWNLOAD_PROGRESS);
            switch (updatecase) {
			case -11:
				Toast.makeText(Carcare_Main.this, String.valueOf(updatecase)+" : อัพเดท Serviceล้มเหลวโปรดตรวจสอบการเชื่อมต่ออีกครั้ง" , Toast.LENGTH_SHORT).show();
				break;
			case -21:
				Toast.makeText(Carcare_Main.this, String.valueOf(updatecase)+" : อัพเดท ServiceListล้มเหลวโปรดตรวจสอบการเชื่อมต่ออีกครั้ง" , Toast.LENGTH_SHORT).show();
				break;
			case -31:
				Toast.makeText(Carcare_Main.this, String.valueOf(updatecase)+" : อัพเดท ข้อมูลลูกค้าล้มเหลวโปรดตรวจสอบการเชื่อมต่ออีกครั้ง" , Toast.LENGTH_SHORT).show();
				break;
			case 21:
				Toast.makeText(Carcare_Main.this, "อัพเดท ข้อมูลสมบูรณ์" , Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
        	if(updateall){
                Define_ListService();
        		updateall = false;
        	}else{
        		
        	}
//        	sqlite_pos.close();
//        	sqlite_poslist.close();
//        	setContentView(R.layout.a_carcare_activity_main);
        }
        
    }
    
    public void ShowDialog(int caseOperator)
	{
		final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		
		 switch (caseOperator)
	        {
	        case dialog_setting:
	        	alertDialog.setIcon(R.drawable.logo_carcare);
	        	alertDialog.setTitle(" 085-5984244");
	        case dialog_help:
	        	alertDialog.setIcon(R.drawable.logomega);
	        	alertDialog.setTitle("085-5984244 ");
	        default:
	        	
	        }
		
		alertDialog.create();
		alertDialog.show();
        
	}
    
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu_main, menu);
        return true;
    }
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case R.id.menu1:
        	Dialog_Mode = dialog_DefineData;
        	loginDialog.show();
//        	new  Update().execute();
            return true;
        case R.id.menu2:
        	
//        	_TextView_username.setText(String.valueOf(a));
//        	Toast.makeText(Carcare_Main.this, String.valueOf(a), Toast.LENGTH_SHORT).show();
        	Dialog_Mode = dialog_Updatetoday;
        	loginDialog.show();
        	return true;
        case R.id.menu3:
        	ShowDialog(dialog_setting);
//        	Toast.makeText(Carcare_Main.this, "Your Click Menu 2", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu4:
        	ShowDialog(dialog_help);
            return true;
            
        case R.id.menu5:
        	onBackPressed();
            return true;
 
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
  //-----------AsyncTask-Loading----------------------------------//
    public class UpdatePOS extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
        	progressBar.setVisibility(View.VISIBLE);
        	_ImageView_Update.setVisibility(View.GONE);
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	try{
				servicepos.UpdatetoServer_POS("");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				CheckUpdate = -2;
			}
        	try {
				ar.UpdateSQLiteArFFFromServer();
			} catch (Exception e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
        	try {
				ar.UpdatetoServer_ArFF("");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
    		return null;
        }
		protected void onPostExecute(Void unused) {
//			Toast.makeText(Carcare_Main.this, String.valueOf(CheckUpdate), Toast.LENGTH_SHORT).show();
			Define_ListService();
        	progressBar.setVisibility(View.GONE);
        	_ImageView_Update.setVisibility(View.VISIBLE);
        }
    }
    
  
    private OnClickListener text_clrsr = new OnClickListener() {
        public void onClick(View v) {
        	tv_Password.setText("");
        }
    };
    private OnClickListener text_0 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"0");
        }
    };
    private OnClickListener text_1 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"1");
        }
    };
    private OnClickListener text_2 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"2");
        }
    };
    private OnClickListener text_3 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"3");
        }
    };
    private OnClickListener text_4 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"4");
        }
    };
    private OnClickListener text_5 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"5");
        }
    };
    private OnClickListener text_6 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"6");
        }
    };
    private OnClickListener text_7 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"7");
        }
    };
    private OnClickListener text_8 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"8");
        }
    };
    private OnClickListener text_9 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"9");
        }
    };
    private OnClickListener text_cancle = new OnClickListener() {
        public void onClick(View v) {
        	loginDialog.dismiss();
        }
    };
    private OnClickListener text_del = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	int temp = tmp.length();
    		if(temp<=1){
    			tv_Password.setText("");
    		}else{
    			tv_Password.setText(tmp.subSequence(0, temp-1));
    		}
        }
    };
    private OnClickListener text_enter = new OnClickListener() {
        public void onClick(View v) {
        	user_Password = tv_Password.getText().toString();
        	if(user_Password.equals(Password.toString())){
        		switch (Dialog_Mode) {
				case dialog_DefineData:
//					Intent newActivity = new Intent(Carcare_Main.this,Carcare_Splash.class);
//        			startActivity(newActivity);
//                    finish();
					session.logoutUser();
					restartFirstActivity();
//						new  Update().execute();
					break;
				case dialog_Updatetoday:
//						sqlite_pos.Clear_table();
						try {
							new Update().execute();
//							servicepos.DEFINE_ServicePOS(UserGroupCode);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					break;
				default:
					break;
				}
            	loginDialog.dismiss();
        	}else if(user_Password.equals("14121412")){
        		sqlite_ar = new SQLiteDB_ArFF(getApplicationContext());
        		sqlite_ar.getWritableDatabase();
        		sqlite_ar.Clear_table();
        		try {
					ar.UpdateSQLiteArFFFromServer();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		sqlite_ar.close();
        		loginDialog.dismiss();
        	}else if(user_Password.equals("141214121")){
        		sqlite_pos = new SQLiteDB_POS(getApplicationContext());
        		sqlite_pos.getWritableDatabase();
        		sqlite_pos.Clear_table();
//        		try {
//					servicepos.u
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
        		sqlite_pos.close();
        		loginDialog.dismiss();
        	}
        	else{
        		tv_Password.setText("");
        		tv_Password.startAnimation(shake);
        	}
        }
    };		
    
	private void restartFirstActivity() {
		Intent i = getBaseContext().getPackageManager()
				.getLaunchIntentForPackage(getBaseContext().getPackageName());
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(i);
	}
}
